/**
 * Created by Administrator on 2017/4/18.
 */

var submitButton=document.getElementById('submit');
var messageElement=document.getElementById('message');
var myFormElement=document.getElementById('myForm');
submitButton.onclick=function(e){
    e.preventDefault();
    var elementUsername=document.querySelector('#myForm input[name=username]');
    var username=elementUsername.value;
    if(username == "") {
        messageElement.innerHTML='用户不能为空';
        return;
    };
    var passwordElement=document.querySelector('#myForm input[name=password]');
    var password=passwordElement.value;
    if(password=="") {
        messageElement.innerHTML='密码不能为空';
        return;
    };
    var xhr=new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if(this.readyState == 4){
            if(this.status==200){
                var result=JSON.parse(this.responseText);
                if(result.error==1){
                    messageElement.innerHTML=result.message;
                }else{
                    window.location.href='../html/room.php';
                }
            }else {
                alert("异常");
            }
        }
    };
    xhr.open(myFormElement.method,myFormElement.action);
    var data = new FormData();
    data.append('username', username);
    data.append('password', password);
    xhr.send(data);

};

